package summer.codegenerator.persistence.hibernate.base.tool;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangz
 * @birth  2017年12月4日 下午10:09:54
 */
public class Condition implements Serializable,WhereClause {

	private static final long serialVersionUID = 1L;

	private String name;

	private String operator;

	private Object operand;
	
	private boolean paramEnable;
	
	private String paramName;

	private String generateOperand() {
		String clazzName = operand.getClass().getSimpleName();
		if (clazzName.equals("String")) {
			return String.format("'%s'", operand);
		} else {
			return operand.toString();
		}
	}

	public Condition(String name, String operator, Object operand) {
		super();
		this.name = name;
		this.operator = operator;
		this.operand = operand;
		this.paramEnable = false;
	}
	
	public Condition(String name, String operator, Object operand, String paramName) {
		super();
		this.name = name;
		this.operator = operator;
		this.operand = operand;
		this.paramEnable = true;
		this.paramName = paramName;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return String.format("%s %s %s", name, operator, generateOperand());
	}

	@Override
	public String getClause() {
		if(this.paramEnable) {
			return String.format(" where %s %s :%s", name, operator, paramName);
		} else {
			return String.format(" where %s %s %s", name, operator, generateOperand());
		}
	}

	@Override
	public Map<String, Object> getParamMap() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(this.paramEnable) {
			paramMap.put(this.paramName, this.operand);
		}
		return paramMap;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public Object getOperand() {
		return operand;
	}

	public void setOperand(Object operand) {
		this.operand = operand;
	}

	public boolean getParamEnable() {
		return paramEnable;
	}
	
	public void setParamEnable(boolean paramEnable) {
		this.paramEnable = paramEnable;
	}

	public String getParamName() {
		return paramName;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}
}
